﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;


public partial class SociableBE_Admin : Page {
  protected static List<string> _services = new List<string>();
  protected static ExtensionSettings _settings;

  protected void Page_Load(object sender, EventArgs e) {
    _services.RemoveAll(rx => rx == string.Empty);
    if (Page.IsPostBack) return;
    _settings = ExtensionManager.GetSettings("SociableBE");
    InitLists();
    BindForm();
  }

  private void InitLists() {
    if (this.sbe_availableServices.Items.Count > 0 || this.sbe_enabledServices.Items.Count > 0) return;
    this.sbe_serviceCount.Text = string.Format("&nbsp;({0} total)", SociableBE.GetServices().Count);
    foreach (SociableBEEntry i in SociableBE.GetServices()) {
      if (!_services.Contains(i.uid.ToString()))
        this.sbe_availableServices.Items.Add(new ListItem(i.name, i.uid.ToString()));
    }
    updateEnabledServiceList();
  }

  private static void InitSettings() {
    var settings = new ExtensionSettings("SociableBE");

    settings.AddParameter("Imagepath");
    settings.AddParameter("Tagline");
    settings.AddParameter("Services");

    settings.AddValue("Imagepath", "/SociableBE/img/");
    settings.AddValue("Tagline", "Share or Bookmark this post&hellip;");
    settings.AddValue("Services", string.Empty);

    settings.IsScalar = true;

    ExtensionManager.SetAdminPage("SociableBE", "~/SociableBE/Admin.aspx");

    ExtensionManager.ImportSettings(settings);
    _settings = ExtensionManager.GetSettings("SociableBE");
  }

  private void BindForm() {
    if (_settings == null)
      InitSettings();

    this.btnSave.Text = labels.saveSettings;
    this.sbe_Imgpath.Text = _settings.GetSingleValue("Imagepath");
    this.sbe_Tagline.Text = _settings.GetSingleValue("Tagline");
    _services.Clear();
    _services.AddRange(_settings.GetSingleValue("Services").Split(';'));
  }

  protected void btnSave_Click(object sender, EventArgs e) {
    _settings.UpdateScalarValue("Imagepath", this.sbe_Imgpath.Text);
    _settings.UpdateScalarValue("Tagline", this.sbe_Tagline.Text);
    var _t = new StringBuilder();
    foreach (string s in _services)
      _t.AppendFormat("{0};", s);
    _settings.UpdateScalarValue("Services", _t.ToString());

    ExtensionManager.SaveSettings("SociableBE", _settings);
    Response.Redirect(Request.RawUrl);
  }

  protected void addService_Click(object sender, EventArgs e) {
    _services.Add(this.sbe_availableServices.SelectedValue);
    updateEnabledServiceList();
  }

  protected void delService_Click(object sender, EventArgs e) {
    _services.Remove(this.sbe_enabledServices.SelectedValue);
    updateEnabledServiceList();
  }

  private void updateEnabledServiceList() {
    this.sbe_enabledServices.Items.Clear();
    foreach (string s in _services) {
      SociableBEEntry _r = SociableBE.GetServices().Where(j => j.uid.ToString() == s).FirstOrDefault();
      this.sbe_enabledServices.Items.Add(new ListItem(_r.name, _r.uid.ToString()));
    }
  }

  protected void pushUp_Click(object sender, EventArgs e) {
    if (this.sbe_enabledServices.SelectedIndex < 1) return;
    string _t = _services[this.sbe_enabledServices.SelectedIndex - 1];
    _services[this.sbe_enabledServices.SelectedIndex - 1] = _services[this.sbe_enabledServices.SelectedIndex];
    _services[this.sbe_enabledServices.SelectedIndex] = _t;
    updateEnabledServiceList();
  }

  protected void pushDown_Click(object sender, EventArgs e) {
    if (this.sbe_enabledServices.SelectedIndex >= this.sbe_enabledServices.Items.Count - 1)
      return;
    string _t = _services[this.sbe_enabledServices.SelectedIndex + 1];
    _services[this.sbe_enabledServices.SelectedIndex + 1] = _services[this.sbe_enabledServices.SelectedIndex];
    _services[this.sbe_enabledServices.SelectedIndex] = _t;
    updateEnabledServiceList();
  }
}